-module(fun_http_action).
-include("common.hrl").

-define(REQ_FAIL,rfc4627:encode({obj, [{"status",fail}]})).
-define(REQ_ERROR,rfc4627:encode({obj, [{"status",error}]})).
-define(REQ_SUCC,rfc4627:encode({obj, [{"status",succ}]})).

-export([usr_ban/3]).

%% send_agentmng(Msg)->gen_server:cast({global,agent_mng}, {data_count,Msg}).
call_agentmng(Msg)->gen_server:call({global,agent_mng}, {data_count,Msg}, 2000).

check_ip(Env,Sid)->
    {remote_addr, Ip} = lists:keyfind(remote_addr, 1, Env),
	case db:get_config(bindips) of
		Addrs when erlang:is_list(Addrs) ->
				
			 case  lists:member(Ip, Addrs) of  
				  true->  true;
				  _->mod_esi:deliver(Sid,[Ip]),false
			 end;
	    _->true
	end.

%%  玩家封号|解除
%% http://host:port/rpc/fun_http_action:usr_ban?uid=Uid&time=Time&action=Action
usr_ban(Sid,Env,In)->
	case check_ip(Env,Sid) of
		true->
			?log_trace("usr_ban~p",[In]),
			KvList = httpd:parse_query(In),
			 
			case util:get_values(["uid","time","action"],KvList) of  
				[Uid,Time,Action]->
					Msg={usr_ban,util:to_integer(Uid),util:to_list(Action),util:to_integer(Time)},
					case  call_agentmng(Msg)  of  
						  ok->mod_esi:deliver(Sid,[?REQ_SUCC]);
                          _->mod_esi:deliver(Sid,[?REQ_FAIL])
					end;
				_->mod_esi:deliver(Sid,[?REQ_ERROR])
			end;
		_->skip
	end.